import { Injectable } from '@angular/core';
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Router } from '@angular/router';
import { apiList } from '../apiList';
import { Observable } from 'rxjs';

@Injectable()
export class AuthInterceptor implements HttpInterceptor {

    constructor( public router: Router,  public api: apiList) {
        // localStorage.setItem('access_token', 'xxxxxxxxxxxxx')
    };
    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        let authReq: any;
        // 实现第一次不拦截的方式：1. 指定接口不拦截  2. 判断本地localStorage
        let loginUrl = this.api.baseurl + this.api.urlList['login']['path'];
        if (req.url !== loginUrl) {
            if (localStorage.getItem('access_token')) {
                // console.log(2);
                const access_token = localStorage.getItem('access_token');
                authReq = req.clone({ setHeaders: { access_token } });
                return next.handle(authReq);
            } else {
                // 未登录  ==  access_token
                this.router.navigate(['/login']);
            }
        }
        authReq = req.clone({ setHeaders: {} });
        return next.handle(authReq);
    }

}
